﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.ComponentModel;

namespace lcmd
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class CmdWindow : Window
	{
		public CmdWindow()
		{
			InitializeComponent();

			LoadDrives();

			directoryLoader = new BackgroundWorker();
			directoryLoader.WorkerReportsProgress = true;
			directoryLoader.WorkerSupportsCancellation = true;
			directoryLoader.ProgressChanged += new ProgressChangedEventHandler(directoryLoader_ProgressChanged);
			directoryLoader.DoWork += new DoWorkEventHandler(directoryLoader_DoWork);
			directoryLoader.RunWorkerCompleted += new RunWorkerCompletedEventHandler(directoryLoader_RunWorkerCompleted);
		}

		void directoryLoader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			if (requestedDirectory != null)
				directoryLoader.RunWorkerAsync();
			else
			{
				progressDirectoryLoader.Visibility = Visibility.Hidden;
				textTime.Text = loadTime.TotalMilliseconds.ToString() + "ms";
			}
		}

		void directoryLoader_DoWork(object sender, DoWorkEventArgs e)
		{
			var start = DateTime.Now;

			showPending.Clear();

			var directory = requestedDirectory;
			requestedDirectory = null;

			var info = new DirectoryInfo(directory);
			var infos = info.GetFileSystemInfos();
			directoryLoader.ReportProgress(0);
			long count = 0;
			foreach (var sub in infos)
			{
				showPending.Add(sub);

				count++;
				directoryLoader.ReportProgress((int)(count * 100 / infos.LongLength));
			}

			loadTime = DateTime.Now - start;
		}

		void directoryLoader_ProgressChanged(object sender, ProgressChangedEventArgs e)
		{
			progressDirectoryLoader.Visibility = Visibility.Visible;
			progressDirectoryLoader.Value = e.ProgressPercentage;
		}

		TimeSpan loadTime;
		BackgroundWorker directoryLoader;
		string requestedDirectory;
		List<FileSystemInfo> showPending = new List<FileSystemInfo>();

		private void LoadDrives()
		{
			stackLogicalDrives.Children.Clear();
			foreach (var drive in DriveInfo.GetDrives())
			{
				var button = new Button();
				if (drive.IsReady)
					button.Content = drive.VolumeLabel;
				else
					button.Content = drive.Name;

				button.Tag = drive;
				button.Click += new RoutedEventHandler(buttonDrive_Click);
				stackLogicalDrives.Children.Add(button);
			}
		}

		private void buttonDrive_Click(object sender, RoutedEventArgs e)
		{
			var button = sender as Button;
			var driveInfo = button.Tag as DriveInfo;
			ChangeDirectory(driveInfo.RootDirectory.FullName);
		}

		private void ChangeDirectory(string target)
		{
			requestedDirectory = target;

			if (directoryLoader.IsBusy)
				directoryLoader.CancelAsync();
			else
				directoryLoader.RunWorkerAsync();
		}

		private void textCurrentPath_KeyDown(object sender, KeyEventArgs e)
		{
			switch (e.Key)
			{
			case Key.Enter:
				ChangeDirectory(textCurrentPath.Text);
				e.Handled = true;
				break;

			default:
				break;
			}
		}
	}
}
